import { Dict } from '@TG/interface/types/global-data'
import { getDataType } from '@TG/kit'
import { isNotEmpty } from '@TG/kit/validate'
import { globalDataModule } from '@TG/store/modules/global-data'

/**
 * 获取字典标签
 * @param field 字段
 * @param value 值
 * @param dictStorer 字典集
 */
export const getDictLabel = (field: string, value: string, dictStorer: Map<string, string | Dict>) => {
  let label: string = ''
  const dict = dictStorer.get(field)
  if (isNotEmpty(dict)) {
    if (getDataType(dict) === 'string') {
      label = globalDataModule.getDictLabel({ type: dict as string, value: value })
    } else {
      label = (dict as Dict).label
    }
  }
  return label
}

/**
 * 获取字典标题
 * @param field 字段
 * @param value 值
 * @param dictStorer 字典集
 */
export const getDictTitle = (field: string, value: string, dictStorer: Map<string, string | Dict>) => {
  const label = getDictLabel(field, value, dictStorer)
  let title: string = ''
  if (isNotEmpty(label)) {
    title = `${label} [${value}]`
  } else {
    title = value
  }
  return title
}
